
/**接口请求封装
 * @author huarunsong 2019-02-20
 * 参数说明
 * opt:
 * * ****name:具体业务请求名称
 * * ****method:默认POST
 * * ****url:接口路径
 * * ****params或data:具体业务请求参数
 * * ****needFailCallback:是否需要单独处理非0000状态的回调函数，true是需要，false默认不需要直接公用方法处理
 * * ****beforeSend:请求前
 * * ****complete:请求后
 */


import router from "../../router";
import urls from '../goUrl.js'
import _axios from "./axios";
import store from "@/store";
import {
    CHANGE_LOADING_STATUS,
    CHANGE_REQUEST_NUM
} from '@/store/mutationTypes';
import $commonJs from "../common.js";
import qs from 'qs';
var axiosHttp = {
    http(opt, callback, callbackFail, callbackNoData, callbackLogin) {
        let that = this;

        let opts = opt || {};

        if (!opts.url) {
            console.error('缺少请求地址');
            return false;
        }

        if (opts.method === undefined) opts.method = 'POST';

        //请求次数+1
        // store.commit(CHANGE_REQUEST_NUM, { type: 'add' })

        if (opts.loading == true && !opts.beforeSend) {
            opts.beforeSend = () => {
                // store.commit(CHANGE_LOADING_STATUS, { isShowLoading: true })
            }
        }

        if (!opts.isShowLoadingTimeout) {
            setTimeout(function () {
                //最长请求时间为10秒 超过10秒后隐藏loading框
                // store.commit(CHANGE_LOADING_STATUS, { 'isShowLoading': false })
            }, 10000)
        }


        if (opts.beforeSend) opts.beforeSend()

        let contentType = 'application/json; charset=UTF-8'
        let data = JSON.stringify(opts.data);

        if (opts.contentTypeSearch) {
            contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
            data = qs.stringify(opts.data);
        }

        return _axios({
            method: opts.method,
            url: opts.url,
            params: opts.params || {},
            data: data,
            headers: {
                'content-type': contentType
            },

            //timeout: 15000,
        }).then(response => {
            if (opts.complete) opts.complete();
            if (!response) return;
            //需要登录的回调
            if (response.data.status == '4007') {
                callbackLogin && callbackLogin(response)
                urls.routerPush(this, urls.login, { redirect: router.currentRoute.fullPath }, true);
            } else
                //1. 接口成功的情况
                // response.data.code == '200' 针对shengcheng接口
                if (response.data.status == '0000' || response.data.code == '200') {
                    callback && callback(response.data)
                }
                else if (response.data.status == '1000') {
                    //无数据，不用黑条提示，执行callbackNoData
                    callbackNoData && callbackNoData(response)
                }
                else {
                    //接口不成功
                    let message = response.data.message ? response.data.message : '系统开小差啦，请联系系统管理员';

                    callbackFail ? callbackFail(response.data) : ($commonJs.toastI(message));
                }


            //请求次数-1
            // store.commit(CHANGE_REQUEST_NUM, { type: 'del' })

            // if (store.state.requestCount <= 0) {
            //     store.commit(CHANGE_LOADING_STATUS, { isShowLoading: false })
            //     store.commit(CHANGE_REQUEST_NUM, { type: 'reset' })
            // }


        }, response => {

            //请求次数-1
            // store.commit(CHANGE_REQUEST_NUM, { type: 'del' })
            // if (store.state.requestCount <= 0) {
            //     store.commit(CHANGE_LOADING_STATUS, { 'isShowLoading': false })
            //     store.commit(CHANGE_REQUEST_NUM, { type: 'reset' })
            // }

            if (opts.complete) opts.complete();

            let message = response.data && response.data.message ? response.data.message : '系统开小差啦，请联系系统管理员';
            response.message = response.data && response.data.message ? response.data.message : '系统开小差啦，请联系系统管理员';

            callbackFail ? callbackFail(response) : ($commonJs.toastI(message));
        }).catch(response => {

            //请求次数-1
            // store.commit(CHANGE_REQUEST_NUM, { type: 'del' })
            // if (store.state.requestCount <= 0) {
            //     store.commit(CHANGE_LOADING_STATUS, { isShowLoading: false })
            //     store.commit(CHANGE_REQUEST_NUM, { type: 'reset' })
            // }

            if (opts.complete) opts.complete();

            console.error((opts.name || '') + "请求异常", response);

        });
    }
}

export default axiosHttp
